import { DataTypes } from 'sequelize';
import { Column, Comment, Index, Model, Table, Unique } from 'sequelize-typescript';

@Table
export class ExampleUpgrade extends Model<ExampleUpgrade> {
  @Comment('实例ID')
  @Unique
  @Column({ type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, allowNull: false })
  example_id: string;

  @Comment('用户ID')
  @Index
  @Column({ allowNull: false })
  account_id: string;

  @Comment('实例名')
  @Column({ allowNull: false })
  example_name: string;

  @Comment('版本号')
  @Column({ type: DataTypes.STRING(50), allowNull: false })
  version: string;

  @Column({
    type: DataTypes.VIRTUAL,
    get() {
      let val = this.getDataValue('version');
      val = val.split('.');
      val = val.map((v: string) => ~~v);
      return val;
    }
  })
  _version: string;

  @Comment('最低版本号')
  @Column({ type: DataTypes.STRING(50), allowNull: false })
  version_min: string;

  @Column({
    type: DataTypes.VIRTUAL,
    get() {
      let val = this.getDataValue('version_min');
      if (!val) return null;
      val = val.split('.');
      val = val.map((v: string) => ~~v);
      return val;
    }
  })
  _version_min: string;

  @Comment('下载地址')
  @Column({ type: DataTypes.STRING(500), allowNull: true })
  download_link: string;

  @Comment('是否强制更新')
  @Column({ type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false })
  force_updating: boolean;

  @Comment('公告')
  @Column({ type: DataTypes.TEXT, allowNull: true })
  notice: string;

  @Comment('备注')
  @Column({ type: DataTypes.TEXT, allowNull: true })
  remark: string;

  @Comment('访问量')
  @Column({ type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 })
  page_view: boolean;
}
